package com.itheima.annotationdemo1;

import org.junit.Test;

import java.lang.reflect.Method;
import java.util.Arrays;

public class AnnotationDemo {

    //解析注解
    @Test
    public void parseClass() throws Exception{

        //1.获取类对象
        Class c1 = Demo.class;
        //2.判断这个类上是否陈列了注解MyTest2
        boolean b = c1.isAnnotationPresent(MyTest2.class);
        if (b) {
            //3.获取注解对象
            MyTest2 annotation = (MyTest2) c1.getDeclaredAnnotation(MyTest2.class);
            //4.获取并打印注解中的属性值
            System.out.println(annotation.value());
            System.out.println(annotation.height());
            System.out.println(Arrays.toString(annotation.address()));

        }

    }

    @Test
    public void parseMethod() throws Exception{
        //1.获取类对象
        Class c1 = Demo.class;
        //2.获取方法对象
        Method m1 = c1.getMethod("go");
        //3.判断这个方法上是否存在注解
        boolean b = m1.isAnnotationPresent(MyTest2.class);
        if (b) {
            //4.获取注解对象
            MyTest2 annotation = m1.getDeclaredAnnotation(MyTest2.class);
            //5.获取并打印注解中的属性值
            System.out.println(annotation.value());
            System.out.println(annotation.height());
            System.out.println(Arrays.toString(annotation.address()));
        }
    }

}
